<template>
  <div class="login-page">
    <el-card>
      <!-- 具名插槽: header -->
      <template #header>
        黑马面经运营后台
      </template>
      <el-form ref="form" :model="form" :rules="rules" >
        <el-form-item label="用户名：" prop="username" >
          <el-input v-model="form.username" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item label="密码：" placeholder="请输入密码" prop="password"> 
           <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
         <el-form-item class="tc">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button  @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
</el-card>
</div>
</template>

<script>
// import store from '@/store'
// import { login } from "@/api/user";
export default {
  name: 'LoginIndex',
  data () {
    return {
       form: {
          username: '',
          password:''
        },
        rules:{
          username:[
            {required:true, message:'请输入用户名', trigger:['blur','change']},
            {min:3,max:10, message:'用户名长度3-5个字符', trigger:['blur','change']}
          ],
          password:[
            {required:true, message:'请输入密码', trigger:['blur','change']},
            {min:3,max:11, message:'密码长度3-11个字符', trigger:['blur','change']}
          ],

        }
    }
  },
  methods: {
   async login(){
      this.$refs.form.validate( async valid=>{if(!valid) return
      // await this.$store.dispatch('user/loginAction',this.form)
      await this.$store.dispatch('user/loginAction',this.form)
      this.$router.push('/')
      this.$message.success('登录成功')
      }
      )
   
   
    },
    reset(){
      this.$refs.form.resetFields()
    }
  }
 }
</script>

<style lang="scss" scoped>
// scoped 只能给当前组件内的所有标签添加 data-v-hash
// 但是, el-card 里面的元素没有被添加这个唯一标识, 所以直接设置样式无法生效
// 解决方案: 深度作用选择器
// less: 在选择器前面添加 /deep/
// scss: 在选择器前面添加 ::v-deep
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
// .el-card {
//   width: 420px;
// }
</style>
